package com.example.widgetcase.slice.viewpager;

import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;

/**
 * 转换器：xy缩放+透明度改变
 */
public class ScaleAlphaTransformer implements PageSlider.PageChangedListener {

    private final float SCALE_MAX = 0.8f;
    private final float ALPHA_MAX = 0.5f;
    PageSlider pageSlider;

    public void setSlider(PageSlider pageSlider) {
        this.pageSlider = pageSlider;
    }

    public void transformPage(Component page, float position) {
        if (page == null) {
            return;
        }

        if ((int) position < -1 || (int) position > 1) {
            return;
        }

        float scale = (position < 0)
                ? ((1 - SCALE_MAX) * position + 1)
                : ((SCALE_MAX - 1) * position + 1);
        float alpha = (position < 0)
                ? ((1 - ALPHA_MAX) * position + 1)
                : ((ALPHA_MAX - 1) * position + 1);

        if (position < 0) { // 目标屏-左边一屏的中心点设置
            page.setPivotX(page.getWidth());
            page.setPivotY(page.getHeight() / 2);
        } else {
            page.setPivotX(0);
            page.setPivotY(page.getHeight() / 2);
        }

        page.setScaleX(scale);
        page.setScaleY(scale);
        page.setAlpha(Math.abs(alpha));
    }

    @Override
    public void onPageSliding(int i, float v, int i1) {

    }

    @Override
    public void onPageSlideStateChanged(int i) {

    }

    @Override
    public void onPageChosen(int i) {

    }
}
